<template>
  <div class="blogs">
    <div class="content-header">
      <h1>用户管理<small>添加用户</small></h1>
      <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
        <el-breadcrumb-item >后台管理</el-breadcrumb-item>
        <el-breadcrumb-item>用户管理</el-breadcrumb-item>
        <el-breadcrumb-item>添加用户</el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    <!--中间内容-->
    <div class="app-container">
		<div class="box">
			<el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px">
				<el-row>
					<el-col :span="10">
					  <el-form-item label="用户账号" prop="id">
						<el-input v-model.trim="ruleForm.id"></el-input>
					  </el-form-item>						
					</el-col>
					<el-col :span="10">
						<el-form-item label="用户密码" prop="password">
						  <el-input v-model.trim="ruleForm.password"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="10">
						<el-form-item label="用户昵称" prop="nick_name">
						  <el-input v-model.trim="ruleForm.nick_name"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="10">
						<el-form-item label="用户邮箱" prop="email">
						  <el-input v-model.trim="ruleForm.email"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="10">
						<el-form-item label="用户权限" prop="power">
						  <el-select v-model="ruleForm.power" placeholder="请选择用户权限">
						    <el-option label="0(无任何权限)" value="0"></el-option>
						    <el-option label="1(仅能发博文)" value="1"></el-option>
							<el-option label="2(拥有所有权限)" value="2"></el-option>
						  </el-select>
						</el-form-item>
					</el-col>
				</el-row>
			  <el-form-item>
			    <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
			    <el-button @click="resetForm('ruleForm')">重置</el-button>
			  </el-form-item>
			</el-form>
		</div>
    </div>
    <br>
    <br>
  </div>
</template>

<script>
  export default {
    data() {
		var validQC = (rule, value, callback) => {
		  if (value) {
			if (/[\u4E00-\u9FA5]/g.test(value)) {
			  callback(new Error("不能输入汉字"));
			} else {
			// 验证通过
			  callback();
			}
			callback();
		  }
		};
        return {
			ruleForm: {
			  id: '',
			  password: '',
			  nick_name: '',
			  email: '',
			  power: null
			},
			rules: {
			  id: [
				{ required: true, message: '请输入用户账号', trigger: 'blur' },
				{validator: validQC, trigger: 'blur'},
				{ min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' },
			  ],
			  password: [
				{ required: true, message: '请输入用户密码', trigger: 'blur' },
				{validator: validQC, trigger: 'blur'},
				{ min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
			  ],
			  nick_name: [
				{ required: true, message: '请输入用户昵称', trigger: 'blur' },
				{ min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
			  ],
			  email: [
				{ required: true, message: '请输入用户邮箱', trigger: 'blur' },
				{validator: validQC, trigger: 'blur'},
				{ min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
			  ],
			  power: [
				{ required: true, message: '请选择用户权限', trigger: 'change' }
			  ]
			}
        };
    },
    methods: {
      submitForm(formName) {
		var that=this
        this.$refs[formName].validate((valid) => {
			//对数据的数据进行检验
          if (valid) {
			  //判断用户是否具有该权限
			  var user=JSON.parse(sessionStorage.getItem("user_info"))
			  if(user.power==2){
				  //校验通过 进行数据添加
				  //120.26.196.115  localhost
				  axios.post("http://"+that.$store.state.host+":8181/user/saveUser",that.ruleForm).then(function(res){
					  //判断是否插入成功！！！！
					  if(res.data.status==true){
						that.$message.success(res.data.message);
						that.resetForm(formName)
					  }else{
						that.$message.error(res.data.message);  
					  }  
				  })
			  }else{
				  that.$message.warning('你不具有该权限!');
			  }
          } else {
            that.$message.error('校验失败，请检查输入格式!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>

<style>
</style>
